package com.magenta.maxoptra.gis.utils;

import com.magenta.maxoptra.gis.GeographicCoordinates;
import com.magenta.maxoptra.gis.GisConstants;

import java.util.Arrays;

/**
 *
 */
public class Coordinates implements GeographicCoordinates {
    private final Double lat;
    private final Double lon;

    public Coordinates (Double lat, Double lon) {
        this.lat = lat;
        this.lon = lon;
    }


    /**
     * Creates a new instance of GeoPosition from the specified
     * latitude and longitude.
     * Each are specified as degrees, minutes, and seconds; not
     * as decimal degrees. Use the other constructor for those.
     *
     * @param latDegrees the degrees part of the current latitude
     * @param latMinutes the minutes part of the current latitude
     * @param latSeconds the seconds part of the current latitude
     * @param lonDegrees the degrees part of the current longitude
     * @param lonMinutes the minutes part of the current longitude
     * @param lonSeconds the seconds part of the current longitude
     */
    public Coordinates(double latDegrees, double latMinutes, double latSeconds,
                       double lonDegrees, double lonMinutes, double lonSeconds) {
        this(latDegrees + (latMinutes + latSeconds / 60.0) / 60.0,
                lonDegrees + (lonMinutes + lonSeconds / 60.0) / 60.0);
    }

    /**
     * Creates a new instance of GeoPosition from the specified
     * latitude and longitude as an array of two doubles, with the
     * latitude first. These are double values in decimal degrees, not
     * degrees, minutes, and seconds.  Use the other constructor for those.
     *
     * @param coordinates latitude and longitude as a double array of length two
     */
    public Coordinates(double[] coordinates) {
        double[] copy = Arrays.copyOf(coordinates, coordinates.length);
        this.lat= copy[0];
        this.lon = copy[1];
    }

    public double getLatitude() {
        return lat;
    }

    public double getLongitude() {
        return lon;
    }

    public int getLatitudeInteger() {
        return (int)(lat * Math.pow(10, GisConstants.MAX_LATITUDE_ACCURACY));
    }

    public int getLongitudeInteger() {
        return (int)(lon * Math.pow(10, GisConstants.MAX_LONGITUDE_ACCURACY));
    }

    /**
     * Returns true the specified GeoPosition and this GeoPosition represent
     * the exact same latitude and longitude coordinates.
     *
     * @param obj a GeoPosition to compare this GeoPosition to
     * @return returns true if the specified GeoPosition is equal to this one
     */
    public boolean equals(Object obj) {
        if (obj instanceof GeographicCoordinates) {
            GeographicCoordinates coordinates = (GeographicCoordinates) obj;
            return coordinates.getLatitude() == lat && coordinates.getLongitude() == lon;
        }
        return false;
    }

    @Override
    public int hashCode() {
        int result;
        long temp;
        temp = lat != +0.0d ? Double.doubleToLongBits(lat) : 0L;
        result = (int) (temp ^ (temp >>> 32));
        temp = lon != +0.0d ? Double.doubleToLongBits(lon) : 0L;
        result = 31 * result + (int) (temp ^ (temp >>> 32));
        return result;
    }

    public String toString() {
        return "[" + lat + ", " + lon + "]";
    }



}
